package com.string;

import java.util.HashMap;

/*
2022-4-11
3. 无重复字符的最长子串
 */
public class LengthOfLongestSubstring {

    public static int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0, start = 0;
        for (int end = 0; end < s.length(); end++) {
            char ch = s.charAt(end);
            if (map.containsKey(ch)) {//若含有重复的，start右移
                start = Math.max(map.get(ch) + 1, start);
            }
            //end - start + 1
            max = Math.max(max, end - start + 1);
            //重复的字符会覆盖之前的
            map.put(ch, end);
        }
        return max;
    }

    public static void main(String[] args) {
        String a = "pwwkew";

        int res = lengthOfLongestSubstring(a);
        System.out.println(res);
        //
        HashMap<Character, Integer> map = new HashMap<>();
        map.put('a', 0);
        map.put('b', 1);
        map.put('c', 2);
        map.put('d', 3);
        System.out.println(a.charAt(0));
        System.out.println(map.get(a.charAt(0)));
    }
}
